<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/th.jpg" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginForm" :model="accountmodel" :rules="loginFormRules" label-width="0px" class="login_form">
        <!--        用户名-->
        <el-form-item prop="username">
          <el-input v-model="accountmodel.loginName" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <!--        密码-->
        <el-form-item prop="password">
          <el-input v-model="accountmodel.password" prefix-icon="el-icon-key" type="password"></el-input>
        </el-form-item>
        <!--登录类型-->
        <el-form-item prop="radio">
          <el-radio-group v-model="accountmodel.radio">
            <el-radio :label="1">管理员</el-radio>
            <el-radio :label="2">员工</el-radio>
            <el-radio :label="3">用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        按钮区域-->
        <el-form-item class="btns">
          <el-button type="register" @click="Register('loginForm')" >用户注册</el-button>
          <el-button type="primary" @click="Login('loginForm')">登录</el-button>
          <el-button type="info" @click="resetLoginForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import jsCookie from 'js-cookie';

export default {
  name:"login",
  data() {
    return {
      // 数据绑定对象
      accountmodel: {
        loginName: 'admin',
        password: '11111',
        radio:''
      },
      // 验证规则对象
      loginFormRules: {
        //验证用户
        loginName: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    Login(formName) {
        this.$refs[formName].validate((valid) =>{
          if(valid){
            const _this = this;//获取整个vue的this
            jsCookie.set('loginName',_this.accountmodel.loginName)

            //  alert('submit!');
            _this.$axios.post("http://localhost:8099/login", _this.accountmodel).then(res => {
              console.log(res.data)
              console.log(res.data.code)

              if (res.data.code=200){

                const jwt = res.headers['authorization'];
                const userInfo = res.data.data

                _this.$store.commit("REMOVE_INFO")
                //存储(共享)全局变量jwt和userInfo
                _this.$store.commit("SET_TOKEN", jwt);
                _this.$store.commit("SET_USERINFO", userInfo);

                //获取token和getUser
                // console.log(_this.$store.getters.getToken)
                // console.log(_this.$store.getters.getUser)
                //跳转blogs页面
                if (userInfo.gender!=null){
                  _this.$router.push('/user');
                }else if (userInfo.name!=null){
                  _this.$router.push('/employee')
                }else {
                  _this.$router.push('manager')
                }

              }else {
                this.$message({showClose:false,message:"请勿重复登录",type:'error'})
              }



            })

          } else {
            console.log('error submit!!');
            return false;
          }
      });
    },
    //重置登录表单
    resetLoginForm() {
      // console.log(this)
      // ui框架自带的form表单方法
      this.accountmodel={loginName: '',password: '',radio: ''}
      this.$message({showClose:true,message:"重置成功",type:'success'})


    },
    Register(){
      this.$router.replace('/register')
    }

  }
}
</script>
<!--加上scoped是将样式应用于此组件，不加是全局-->
<style lang="less" scoped>
.login_container {
  background-color: cadetblue;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: white;
  border-radius: 3px;
  /*容器内居中*/
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 2px solid #eee;
    border-radius: 50%;
    padding: 10px;
    /*边框阴影*/
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
